package com.group4.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group4.edu.entity.Course;
import com.group4.edu.entity.CourseDescription;
import com.group4.edu.mapper.CourseMapper;
import com.group4.edu.service.ChapterService;
import com.group4.edu.service.CourseDescriptionService;
import com.group4.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group4.edu.service.VideoService;
import com.study.vo.CourseInfoVO;
import com.study.vo.CoursePublishVO;
import com.study.vo.CourseQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;

    @Override
    public CourseInfoVO getById(String id) {
        CourseInfoVO courseInfoVO = new CourseInfoVO();
        Course course = baseMapper.selectById(id);
        BeanUtils.copyProperties(course,courseInfoVO);

        CourseDescription courseDescription = courseDescriptionService.getById(id);
        courseInfoVO.setCourseDescription(courseDescription.getDescription());
        return courseInfoVO;
    }

    @Override
    public int updateBuyCount(String id) {
        Course course = baseMapper.selectById(id);
        course.setBuyCount(course.getBuyCount()+1);
        return baseMapper.updateById(course);
    }

    @Override
    @Transactional
    public String add(CourseInfoVO courseInfoVO) {
        //插入课程
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVO,course);
        baseMapper.insert(course);

        //插入课程描述
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoVO.getCourseDescription());
        courseDescription.setId(course.getId());
        courseDescriptionService.save(courseDescription);

        return course.getId();
    }

    @Transactional
    @Override
    public void updateCourseInfoById(CourseInfoVO courseInfoVO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVO,course);
        baseMapper.updateById(course);

        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties(courseInfoVO,courseDescription);
        courseDescription.setDescription(courseInfoVO.getCourseDescription());
        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVO getCoursePublishInfoById(String id) {
        return courseMapper.getCoursePublishInfoById(id);
    }

    @Override
    public void publishCourseById(String id) {
        Course course = new Course();
        course.setId(id);
        course.setStatus(Course.COURSE_NORMAL);
        baseMapper.updateById(course);
    }

    @Override
    public void pageQuery(Page<Course> pageParam, CourseQueryVO courseQuery) {

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.ge("subject_id", subjectId);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public List<Course> getNewestCourse() {
        QueryWrapper<Course> qw = new QueryWrapper<>();
        qw.orderByDesc("gmt_create");
        qw.last("limit 8");
        baseMapper.selectList(qw);
        return null;
    }

    @Override
    @Transactional
    public void removeCourseById(String id) {
        baseMapper.deleteById(id);
        chapterService.deleteChapterByCourseId(id);
        videoService.deleteVideoByCourseId(id);
    }

}
